package com.zhengsr.wanandroid.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zhengsr.corelib.bean.LoginBean;
import com.zhengsr.wanandroid.R;
import com.zhengsr.wanandroid.mvp.base.BaseActivity;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.present.LoginPresent;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhengshaorui
 * time: 2018/10/7
 */

public class LoginInActivity extends BaseActivity<LoginPresent> implements ContractManager.ILoginView {

    private String mErrorMsg;

    private LoginPresent mLoginPresent;

    @BindView(R.id.common_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.login_in_name)
    EditText mNameEt;
    @BindView(R.id.login_in_password)
    EditText mPasswordEt;


    @OnClick({R.id.login_in_btn,R.id.login_up_tv})
    void onClickEvent(View view){
        if (view.getId() == R.id.login_in_btn){
            if (checkNull()){
                String name = mNameEt.getText().toString().trim();
                String password = mPasswordEt.getText().toString().trim();
                mLoginPresent.getLoginIn(name,password);
            }else{
                Toast.makeText(this, mErrorMsg, Toast.LENGTH_SHORT).show();
                mErrorMsg = "";
            }
        }
        if (view.getId() == R.id.login_up_tv){
            startActivity(new Intent(this,LoginUpActivity.class));
        }
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_loginin;
    }

    @Override
    public LoginPresent getPresent() {
        mLoginPresent = LoginPresent.create(this);
        return mLoginPresent;
    }

    @Override
    public void initView() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    private boolean checkNull() {
        boolean ischeck = true;
        String name = mNameEt.getText().toString();
        String password = mPasswordEt.getText().toString();
        if (name.isEmpty() || password.isEmpty()){
            ischeck = false;
            mErrorMsg = "用户名或者密码不能为空";
        }

        return ischeck;
    }

    @Override
    public void loginInSuccess(LoginBean bean) {
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        finish();
    }


}
